import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import '../config/httpHeaders.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() {
    return _HomePageState();
  }
}

class _HomePageState extends State<HomePage> {
  String showText = '还没有请求数据';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('HomePage'),
        elevation: 0.0,
        centerTitle: true,
      ),

      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            RaisedButton(
              onPressed: _jile,
              child: Text('请求数据'),
            ),
            Text(showText)
          ],
        ),
      ),
    );
  }

  void _jile () {
    getHttp().then((val) {
      setState(() {
        showText = val.toString();
      });
    });
  }

  Future getHttp() async{
    try {
      Response response;
      Dio dio = Dio();
      dio.options.headers = HttpHeader;
      response = await dio.get('https://static001.geekbang.org/static/time/menu/data.json?v=27041258');
      return response.data;
    }catch(e){
      return print(e);
    }
  }
}